package com.yingxin.yx.framework.jwt;


import com.yingxin.yx.framework.user.exception.UserAuthException;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author fanchao
 */
@Slf4j
public class JwtHelper {

    public static JwtPayload getJwtPayload(String token) {
        try {
            return JwtFactory.getSubFromTokenWithoutSign(token);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new UserAuthException();

        }

    }

    public static void verifyAccessToken(String jwtPrivateKey, String accessToken) {
        try {
            JwtFactory.parserJwtTokenSubject(jwtPrivateKey, accessToken);
        } catch (SignatureException e) {
            throw new UserAuthException();
        } catch (ExpiredJwtException e) {
            throw e;
        } catch (Exception e) {
            throw new UserAuthException();
        }
    }


    public static void verifyRefreshToken(String jwtPrivateKey, String refreshToken) {
        try {
            JwtFactory.parserJwtTokenSubject(jwtPrivateKey, refreshToken);
        } catch (SignatureException e) {
            throw new UserAuthException();
        } catch (ExpiredJwtException e) {
            throw new UserAuthException();
        } catch (Exception e) {
            throw new UserAuthException();
        }
    }

}
